#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_bt.h"
#include "esp_log.h"
#include "esp_blufi_api.h"
#include "esp_blufi.h"
#include "cJSON.h"
#include "led.h"
#include <netdb.h>
#include "nvs_flash.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "inirw.h"

/* TCP-CLIENT */
#include <sys/param.h>
#include "esp_event.h"
#include "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "esp_task_wdt.h"

/* UART */
#include "driver/gpio.h"
#include "driver/uart.h"

#include "driver/twai.h"

#include "gatts_table_creat_demo.h"

/* The event group allows multiple bits for each event, but we only care about two events:
 * - we are connected to the AP with an IP
 * - we failed to connect after the maximum amount of retries */
#define SOCKET_DISCONNECT_BIT BIT0 // need to connect2server again

#define BLUFI_EXAMPLE_TAG "BLUFI_EXAMPLE"
#define BLUFI_INFO(fmt, ...)   ESP_LOGI(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__)
#define BLUFI_ERROR(fmt, ...)  ESP_LOGE(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__)

/* ecuwifi */
#define TXD_PIN (GPIO_NUM_18)
#define RXD_PIN (GPIO_NUM_19)
/*
    测试用ecu-wifi板txrx引脚
*/
// #define TXD_PIN (GPIO_NUM_4)
// #define RXD_PIN (GPIO_NUM_5)

#define TX_QUEUE_LEN 8
#define TWAI_QUEUE_LEN 10

void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len);
int blufi_security_init(void);
void blufi_security_deinit(void);
esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *callbacks);
